#include <iostream>
#include <signal.h>

using namespace std;

void handler(int signo)
{
    cout << "System signal: " << signo << " replaced by handler!" << endl;
    exit(signo);
}

int main()
{
    signal(SIGALRM, handler);    // handler(SIGALRM)
    alarm(10);

    for(int i = 0; i < 20; i++)
    {
        if(i == 2)  // reset the countdown which will call handler function
        {
            cout << "do alarm..." << endl;
            cout << "Residual time: " << alarm(5) << endl;
        }
        cout << "alarm_test is running..." << endl;
        sleep(1);
    }
    return 0;
}